﻿/****************************************************************************************
*	Mouzetrap - client/server framework	
*
*	$HeadURL: http://mouzetrap.googlecode.com/svn/trunk/Mouzetrap/Network/NetworkHelper.cs $
*	$Id: NetworkHelper.cs 11 2009-06-11 09:10:25Z tobz@fallenrealms.net $
* 
*	Copyright (c) 2008 Toby Lawrence
* 
*	Permission is hereby granted, free of charge, to any person obtaining a copy
*	of this software and associated documentation files (the "Software"), to deal
*	in the Software without restriction, including without limitation the rights
*	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the Software is
*	furnished to do so, subject to the following conditions:
* 
*	The above copyright notice and this permission notice shall be included in
*	all copies or substantial portions of the Software.
* 
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*	THE SOFTWARE.
* 
****************************************************************************************/

using System.Net.Sockets;
using System.Text;

namespace Mouzetrap.Network
{
	/// <summary>
	/// Helper class for miscellaneous network functions.
	/// </summary>
	public static class NetworkHelper
	{
		/// <summary>
		/// The default encoding for reading characters/strings in packets.
		/// </summary>
		public static readonly Encoding DefaultEncoding = Encoding.UTF8;

		/// <summary>
		/// Grabs a new <see cref="SocketAsyncEventArgs" /> instance.
		/// </summary>
		/// <returns>a new instance of the socket args</returns>
		public static SocketAsyncEventArgs AcquireSocketArgs()
		{
			//Interlocked.Increment(ref s_OutstandingArgs);
			//Interlocked.Increment(ref s_AcquiredArgs);
			//Console.WriteLine("Acquiring SocketAsyncEventArg {0}:{1}", s_OutstandingArgs, s_AcquiredArgs);
			//SocketAsyncEventArgs args = ObjectPoolMgr.ObtainObject<SocketAsyncEventArgs>();

			return new SocketAsyncEventArgs();
		}

		/// <summary>
		/// Returns a <see cref="SocketAsyncEventArgs" /> instance to the object pool.
		/// </summary>
		/// <param name="arg">the socket args object to return</param>
		public static void ReleaseSocketArgs(SocketAsyncEventArgs arg)
		{
			//Interlocked.Increment(ref s_ReleasedArgs);
			//Interlocked.Decrement(ref s_OutstandingArgs);
			//Console.WriteLine("Releasing SocketAsyncEventArg {0}:{1}", s_OutstandingArgs, s_ReleasedArgs);

			// ObjectPoolMgr.ReleaseObject<SocketAsyncEventArgs>(arg);
		}

		/// <summary>
		/// Disables the Nagle algorithm on a <see cref="Socket" /> instance.
		/// </summary>
		/// <param name="socket">the socket to change</param>
		public static void SetListenSocketOptions(Socket socket)
		{
			socket.NoDelay = true;
		}
	}
}